<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    * {
      padding: 0;
      margin: 0;
    }

    @font-face {
      font-family: "clockIcon";
      src: url(../font/clockicons.woff2);
    }

    .clockBox {
      background-color: grey;
      text-align: center;
      width: max-content;
      margin: 100px auto;
      padding: 10px;
      border-radius: 4px;
    }

    .clockTitle {
      font-size: 24px;
    }

    .clockTime {
      font-family: "clockIcon";
      font-size: 48px;
      line-height: 48px;
    }

    .clockDate {
      font-size: 24px;
    }
  </style>
</head>

<body>
  <div class="clockBox">
    <div class="clockTitle">现在时间</div>
    <div class="clockTime">
      <span class="hour">00</span>
      <span>:</span>
      <span class="minute">00</span>
      <span>:</span>
      <span class="second">00</span>
    </div>
    <div class="clockDate">

    </div>
  </div>
</body>
<script>

  // 1. 获取元素
  var hourSpan = document.getElementsByClassName("hour")[0];
  var minuteSpan = document.getElementsByClassName("minute")[0];
  var secondSpan = document.getElementsByClassName("second")[0];
  var clockDateDiv = document.getElementsByClassName("clockDate")[0];


  showTime();
  setInterval(showTime, 1000);

  function showTime() {
    var date = new Date();

    var year = date.getFullYear();
    var month = date.getMonth() + 1;
    var day = date.getDate();

    var hour = date.getHours();
    var minute = date.getMinutes();
    var second = date.getSeconds();

    hourSpan.innerText = beauty(hour);
    minuteSpan.innerText = beauty(minute);
    secondSpan.innerText = beauty(second);

    clockDateDiv.innerText = `${year}年${month}年${day}月`;
  }



  function beauty(num) {
    return num < 10 ? "0" + num : num;
  }

</script>

</html>